/*
 * Copyright (c) 2025 STMROS Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <cstdint>
#include <cstddef>
#include <vector>

namespace stmconn {

// 从上下文导入消息ID定义

#pragma pack(push, 1)  // 1字节对齐

// 消息ID定义
enum class MessageId : uint8_t {
@[for name, id in STM_MESSAGE_IDS.items()]
    @(name) = @(id),
@[end for]
};

// 心跳消息结构
struct Heartbeat {
    uint8_t system_id;      // 系统ID
    uint8_t component_id;   // 组件ID
    uint8_t mode;           // 模式
    uint8_t state;          // 状态
    uint32_t time_boot_ms;  // 启动时间(毫秒)
};

// 命令消息结构
struct Command {
    uint16_t command_id;    // 命令ID
    uint8_t param1;         // 参数1
    uint8_t param2;         // 参数2
    uint8_t param3;         // 参数3
    uint8_t param4;         // 参数4
    uint16_t param5;        // 参数5
    uint16_t param6;        // 参数6
    uint32_t param7;        // 参数7
};

// 状态消息结构
struct Status {
    uint8_t system_status;  // 系统状态
    uint8_t battery_status; // 电池状态
    uint16_t error_code;    // 错误码
    uint32_t voltage;       // 电压(mV)
    int16_t temperature;    // 温度(°C)
};

// 传感器数据消息结构
struct SensorData {
    uint32_t time_usec;     // 时间戳(微秒)
    float accel_x;          // 加速度X(m/s²)
    float accel_y;          // 加速度Y(m/s²)
    float accel_z;          // 加速度Z(m/s²)
    float gyro_x;           // 角速度X(rad/s)
    float gyro_y;           // 角速度Y(rad/s)
    float gyro_z;           // 角速度Z(rad/s)
    float mag_x;            // 磁场X(G)
    float mag_y;            // 磁场Y(G)
    float mag_z;            // 磁场Z(G)
    float pressure;         // 气压(hPa)
    float altitude;         // 高度(m)
    float temperature;      // 温度(°C)
};

// 电机控制消息结构
struct MotorControl {
    uint8_t motor_id;       // 电机ID
    int16_t throttle;       // 油门值(-1000~1000)
    uint16_t pwm_value;     // PWM值(0~2000)
};

// 序列化函数
size_t serialize_heartbeat(const Heartbeat &msg, std::vector<uint8_t> &buffer);
size_t serialize_command(const Command &msg, std::vector<uint8_t> &buffer);
size_t serialize_status(const Status &msg, std::vector<uint8_t> &buffer);
size_t serialize_sensor_data(const SensorData &msg, std::vector<uint8_t> &buffer);
size_t serialize_motor_control(const MotorControl &msg, std::vector<uint8_t> &buffer);

// 反序列化函数
bool deserialize_heartbeat(const std::vector<uint8_t> &buffer, Heartbeat &msg);
bool deserialize_command(const std::vector<uint8_t> &buffer, Command &msg);
bool deserialize_status(const std::vector<uint8_t> &buffer, Status &msg);
bool deserialize_sensor_data(const std::vector<uint8_t> &buffer, SensorData &msg);
bool deserialize_motor_control(const std::vector<uint8_t> &buffer, MotorControl &msg);

} // namespace stmconn